package com.example.tms.mapper;


import com.example.tms.bean.WaybillMgmt;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface WaybillMapper extends JpaRepository<WaybillMgmt,String>, JpaSpecificationExecutor<WaybillMgmt> {

    //查询运单信息（运单id）
    @Query(value = "select * from waybill_mgmt where ydid = ?1 and sfcz = true", nativeQuery = true)
    WaybillMgmt findByYdid(String ydid);

    //查询运单信息（运单号）
    @Query(value = "select * from waybill_mgmt where ydh = ?1 and sfcz = true", nativeQuery = true)
    WaybillMgmt findByYdh(String ydh);

    //运单发车
    @Query(value = "update waybill_mgmt set ydzt = '已发车'where ydid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void startWaybill(String ydid);

    //删除运单
    @Query(value = "update waybill_mgmt set sfcz = false where ydid = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void deleteWaybill(String ydid);

    //删除运单
    @Query(value = "update waybill_mgmt set sfcz = false where ydh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void deleteWaybill2(String ydh);

    //修改运单状态（运单号）
    @Query(value = "update waybill_mgmt set ydzt = ?2 where ydh = ?1", nativeQuery = true)
    @Modifying
    @Transactional
    void modifyWaybill(String ydh, String ydzt);

    //查询当天运单数（运单日期）
    Long countByYdrq(String ydrq);

    //查询运单信息（线路名称 状态）
    @Query(value = "select * from waybill_mgmt where xlmc = ?1 and sfcz = true and ydzt = '待调度'", nativeQuery = true)
    List<WaybillMgmt> findByXlidandYdzt1(String xlmc);
    @Query(value = "select * from waybill_mgmt where xlmc = ?1 and sfcz = true and ydzt = '审核中'", nativeQuery = true)
    List<WaybillMgmt> findByXlidandYdzt2(String xlmc);
    @Query(value = "select * from waybill_mgmt where xlmc = ?1 and sfcz = true and ydzt = '待发车'", nativeQuery = true)
    List<WaybillMgmt> findByXlidandYdzt3(String xlmc);
    @Query(value = "select * from waybill_mgmt where xlmc = ?1 and sfcz = true and ydzt = '已发车'", nativeQuery = true)
    List<WaybillMgmt> findByXlidandYdzt4(String xlmc);
    @Query(value = "select * from waybill_mgmt where xlmc = ?1 and sfcz = true and ydzt = '已回车'", nativeQuery = true)
    List<WaybillMgmt> findByXlidandYdzt5(String xlmc);

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='待调度' and sfcz = true", nativeQuery = true)
    Integer findYdddd();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='待发车' and sfcz = true", nativeQuery = true)
    Integer findYddfc();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='已完成' and sfcz = true", nativeQuery = true)
    Integer findYdywc();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='已回车' and sfcz = true", nativeQuery = true)
    Integer findYdyhc();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='已发车' and sfcz = true ", nativeQuery = true)
    Integer findYdyfc();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='撤销' and sfcz = true ", nativeQuery = true)
    Integer findYdcx();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='已报账' and sfcz = true", nativeQuery = true)
    Integer findYdybz();

    //查询车辆数量
    @Query(value = "select  count(ydid)  from account where ydzt='已报' and sfcz = true", nativeQuery = true)
    Integer findYdyb();

    //查询车辆数量
    @Query(value = "select COUNT(ydid) from account WHERE ydzt like CONCAT('%','审核中','%')  and sfcz = true", nativeQuery = true)
    Integer findYdshz();
}
